package com.liujit.minio.service;

import com.liujit.minio.properties.MinioProperties;
import com.liujit.minio.domain.BucketDetail;
import com.liujit.minio.domain.FileDetail;
import io.minio.*;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: liujun
 * @date: 2021/6/19 12:59 上午
 * @description: 文件对象操作Service
 */
@Slf4j
@Configuration
@EnableConfigurationProperties({ MinioProperties.class })
public class BucketService implements InitializingBean {

    private MinioProperties minioProperties;

    private MinioClient client;

    public BucketService(MinioProperties minioProperties) {
        this.minioProperties = minioProperties;
    }

    @Override
    public void afterPropertiesSet() {
        this.client = new MinioClient.Builder()
                .endpoint(minioProperties.getEndpoint(), Integer.valueOf(minioProperties.getPort()), false)
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey()).build();
    }

    /**
     * 创建一个新的存储桶
     *
     * @param bucketName
     * @return
     * @author: liujun
     * @date: 2021/6/19 12:59 上午
     */
    @SneakyThrows
    public Boolean makeBucket(String bucketName) {
        if (this.bucketExists(bucketName)) {
            return Boolean.FALSE;
        }
        MakeBucketArgs makeBucketArgs = MakeBucketArgs.builder().bucket(bucketName).build();
        client.makeBucket(makeBucketArgs);
        return Boolean.TRUE;
    }

    /**
     * 列出所有存储桶
     *
     * @return
     * @author: liujun
     * @date: 2021/6/20 10:22 下午
     */
    @SneakyThrows
    public List<BucketDetail> listBuckets() {
        List<BucketDetail> bucketDetails = new ArrayList<>();
        ListBucketsArgs listBucketsArgs = ListBucketsArgs.builder().build();
        List<Bucket> buckets = client.listBuckets(listBucketsArgs);
        buckets.forEach(item -> {
            bucketDetails.add(new BucketDetail() {{
                setName(item.name());
                setCreateTime(Date.from(item.creationDate().toInstant()));
            }});
        });
        return bucketDetails;
    }

    /**
     * 检查存储桶是否存在
     *
     * @param bucketName
     * @return
     * @author: liujun
     * @date: 2021/6/20 10:34 下午
     */
    @SneakyThrows
    public Boolean bucketExists(String bucketName) {
        BucketExistsArgs bucketExistsArgs = BucketExistsArgs.builder().bucket(bucketName).build();
        return client.bucketExists(bucketExistsArgs);
    }

    /**
     * 列出某个存储桶中的所有对象
     *
     * @param bucketName
     * @param prefix
     * @return
     * @author: liujun
     * @date: 2021/6/20 10:28 下午
     */
    @SneakyThrows
    public List<FileDetail> listObjects(String bucketName, String prefix) {
        List<FileDetail> fileDetails = new ArrayList<>();
        if (this.bucketExists(bucketName)) {
            ListObjectsArgs listObjectsArgs = ListObjectsArgs.builder().bucket(bucketName).prefix(prefix).build();
            Iterable<Result<Item>> myObjects = client.listObjects(listObjectsArgs);
            for (Result<Item> result : myObjects) {
                Item item = result.get();
                fileDetails.add(new FileDetail() {{
                    setPath(item.objectName());
                    setSize(item.size());
                    setUploadTime(Date.from(item.lastModified().toInstant()));
                }});
            }
        }
        return fileDetails;
    }
}
